/*
 * Copyright 2008 Jens Meiss
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package mtt.intro;

import java.io.IOException;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.ImageItem;
import javax.microedition.lcdui.Item;

import mtt.intro.events.ShowIntroViewEvent;
import mtt.menu.events.ShowMainMenuEvent;
import hmvc.j2me.controller.AController;
import hmvc.j2me.events.IEvent;
import hmvc.j2me.events.ShowDisplayAbleEvent;
import hmvc.j2me.events.TerminateEvent;

/**
 * @author jens.meiss
 *
 */
public class IntroController extends AController implements CommandListener {

	private IntroView view;
	
	public IntroController() {
		
		setView(new IntroView());
	}
	
	private IntroView getView() {
		return view;
	}

	private void setView(IntroView view) {
		this.view = view;
	}

	public void init() {
		
		try {
			setView(new IntroView());
			
			Form form = new Form("Mobile-Time-Table");
			form.setCommandListener(this);
			getView().setForm(form);
			
			Command commandStart = new Command("Start", Command.SCREEN, 0);
			getView().setCommandStart(commandStart);
			
			ImageItem imageWelcome = new ImageItem(null, Image.createImage("/images/welcome.png"), Item.LAYOUT_CENTER, "Welcome to the MobileTimeTable");
			getView().setImageWelcome(imageWelcome);
		
			form.append(imageWelcome);			
			form.addCommand(commandStart);			
			form.setCommandListener(this);
			
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		register(ShowIntroViewEvent.class);
	}

	public void terminate() {
	}

	public void commandAction(Command arg0, Displayable arg1) {
		
		fireEventUp(new ShowMainMenuEvent());
		fireEvent(new TerminateEvent(), false, true, false);
	}

	public void receiveEvent(IEvent event) {
		
		if (event instanceof ShowIntroViewEvent) {
			fireEventUp(new ShowDisplayAbleEvent(getView().getDisplayable()));
		}	
	}
}
